package com.luckynote.common.base.config;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.luckynote.common.base.dubbo.LogEnum;
import com.luckynote.common.base.util.StringUtil;
import org.slf4j.MDC;

/**
 * MDC转换器
 *
 * @author: Leopold
 * @date: 2019/7/8 11:35
 */
public class MDCConverter extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent event) {
        String traceId = MDC.get(LogEnum.TRACE_ID.getCode());
        String moduleName = MDC.get(LogEnum.MODULE_NAME.getCode());
        String MDCStr = "";
        boolean flag = Boolean.FALSE;
        if (StringUtil.isNotEmptyString(traceId)) {
            MDCStr = MDCStr.concat("[" + traceId + "]-[" + moduleName + "]");
            flag = Boolean.TRUE;
        }
        if (StringUtil.isNotEmptyString(moduleName)) {
            if (flag) {
                MDCStr = MDCStr.concat("-");
            }
            MDCStr = MDCStr.concat("[").concat(moduleName).concat("]");
        }
        return MDCStr;
    }
}
